package com.lastcrdg.testmvp.login;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lastcrdg.testmvp.BaseMVPActivity;
import com.lastcrdg.testmvp.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/8/17 17:36
 * description：
 */

public class LoginActivity extends BaseMVPActivity<LoginActivity, LoginPresenterImp> implements ILoginActivity {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn)
    Button btn;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @Override
    protected LoginPresenterImp createPresenter() {
        return new LoginPresenterImp();
    }

    @Override
    public void onSuccess(String msg) {
        clear();
        btn.setClickable(true);
        dismissProgress();
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onfail(String msg) {

    }

    @Override
    public void clear() {
        etName.setText("");
        etPwd.setText("");
    }

    @Override
    public void showProgress() {
        dialog = new ProgressDialog(this);
        dialog.setTitle("logining...");
        dialog.show();
    }

    @Override
    public void dismissProgress() {
        if (dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        btn.setClickable(false);
        showProgress();
        mPresenter.login(etName.getText().toString(), etPwd.getText().toString());
    }
}
